package com.ssi.mrms.action.room;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.Result;

import com.ssi.mrms.action.BaseAction;
import com.ssi.mrms.model.Msg;
import com.ssi.mrms.model.RMfrudtlModel;
import com.ssi.mrms.model.RMfruhdrModel;
import com.ssi.mrms.model.Msg.MsgType;
import com.ssi.mrms.service.FunctionRoomUsage;


@Namespaces(value = { @Namespace("/room") })
public class RM005AAction extends BaseAction {

	private static final long serialVersionUID = -6884064248574281944L;

	private static final Logger logger = Logger.getLogger(RM005AAction.class);
	
	private FunctionRoomUsage functionRoomUsage = lookupBean(FunctionRoomUsage.class, "FunctionRoomUsage");

	private RMfrudtlModel mfrudtlModel = null;
	private RMfruhdrModel mfruhdrModel = null;
	private Integer pageId; 
	
	private String mode;

	@Action(value = "/RM005A", results = { @Result(name = "success", location = "RM005A.tiles", type = "tiles") })
	public String execute() throws Exception {
		logger.info("## "+this.getClass().getSimpleName());
		
		return SUCCESS;
	}
	@Action(value = "/RM005Aadd", results = { @Result(name = "success", location = "RM005A.tiles", type = "tiles"),
											  @Result(name = "go", location = "RM005", type = "redirect" ,
											  params={"msg.msg","S001"} )})
	public String add() throws Exception {
		logger.info("## "+this.getClass().getSimpleName());
		
		logger.info(mfrudtlModel);
		logger.info(mfruhdrModel);
		
		mfruhdrModel.setrMfrudtlModel(mfrudtlModel);

		try{
			if(StringUtils.isBlank(mode) || mode.equals("add")){
				logger.info("@@@@@@@ add ");
				
				functionRoomUsage.create(mfruhdrModel);
				
				return "go";
				
			}else if (mode.equals("edit")){
				logger.info("####### edit ");
				
				functionRoomUsage.update(mfruhdrModel);
				
				return "go";
				
			}
			
		}catch( Exception e){
			e.printStackTrace();
			Msg msg = new Msg();
			msg.setMsg( e.getMessage());
			msg.setType(MsgType.ERROR);
			
			super.setMsg(msg);
			
		}
		
		return SUCCESS;
	}
	@Action(value = "/RM005Aedit", results = { @Result(name = "success", location = "RM005A.tiles", type = "tiles") })
	public String edit() throws Exception {
		logger.info("##edit "+this.getClass().getSimpleName());
		mode = "edit";
		logger.info("PageId : " + pageId);
		if(pageId == null){
			logger.info("isEmpty ");
			return SUCCESS;
		}
			
		RMfruhdrModel re = functionRoomUsage.findById(pageId);
		if(re != null){
			mfruhdrModel = re;
			mfrudtlModel = re.getrMfrudtlModel();
		}else{
			logger.info("pageId : " + pageId + " :> Not Found" );
		}
		
		
		return SUCCESS;
	}
	@Action(value = "/RM005Adelete", results = { @Result(name = "success", location = "RM005A.tiles", type = "tiles"),
												@Result(name = "go", location = "RM005", type = "redirect",
												params={"msg.msg","D001"} ) })
	public String delete() throws Exception {
		logger.info("## delete "+this.getClass().getSimpleName());
		mode = "delete";
		
		logger.info("PageId : " + pageId);
		if(pageId == null){
			logger.info("isEmpty ");
			return SUCCESS;
		}
			
		RMfruhdrModel re = functionRoomUsage.findById(pageId);
		if(re != null){
			mfruhdrModel = re;
			mfrudtlModel = re.getrMfrudtlModel();
			functionRoomUsage.delete(mfruhdrModel);
			
			return "go";
			
		}else{
			logger.info("pageId : " + pageId + " :> Not Found" );
		}
		
		return SUCCESS;
	}
	public RMfrudtlModel getMfrudtlModel() {
		return mfrudtlModel;
	}
	public void setMfrudtlModel(RMfrudtlModel mfrudtlModel) {
		this.mfrudtlModel = mfrudtlModel;
	}
	public RMfruhdrModel getMfruhdrModel() {
		return mfruhdrModel;
	}
	public void setMfruhdrModel(RMfruhdrModel mfruhdrModel) {
		this.mfruhdrModel = mfruhdrModel;
	}
	public String getMode() {
		return mode;
	}
	public void setMode(String mode) {
		this.mode = mode;
	}
	public Integer getPageId() {
		return pageId;
	}
	public void setPageId(Integer pageId) {
		this.pageId = pageId;
	}
	
	
}